﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS.Domain.Services
{
    public class DeleteService
    {
        public CPPEI.ServiceResult DeleteMedia(Guid id)
        {
            using(CMSDbContext dbContext = new CMSDbContext())
            {
                EntityRepository<Entities.EMedia> rep = new EntityRepository<Entities.EMedia>(dbContext);

                Entities.EMedia media = rep.Get(p => p.Id == id);
                if(media != null)
                {
                    rep.Delete(media);
                    if (!string.IsNullOrEmpty(media.FilePath))
                    {
                        try
                        {
                            File.Delete(media.FilePath);
                        }
                        catch (Exception ex)
                        {

                        }
                    }


                    try
                    {
                        dbContext.SaveChanges();
                        return new CPPEI.ServiceResult();
                    }
                    catch (Exception ex)
                    {
                        return new CPPEI.ServiceResult(0, ex.Message);
                    }
                }
                return new CPPEI.ServiceResult();
            }
        }
    }
}
